package com.itcam.thread_.lock;

public class ThreadTest1 {
    /*
        需求：某电影院目前正在上映国产大片，共有100张票，而它有3个窗口卖票，请设计一个程序模拟该电影院卖票
                - 多条线程共享操作同一份资源
     */
    public static void main(String[] args) {
        TicketTask task = new TicketTask();

        Thread t1 = new Thread(task, "窗口1");
        Thread t2 = new Thread(task, "窗口2");
        Thread t3 = new Thread(task, "窗口3");

        t1.start();
        t2.start();
        t3.start();

    }
}

class TicketTask implements Runnable {

    private Object o = new Object();

    private int tickets = 2000;

    @Override
    public void run() {
        while (true) {
            synchronized (o) {
                if (tickets == 0) {
                    break;
                }

                System.out.println(Thread.currentThread().getName() + "卖出了第" + tickets + "号票");
                tickets--;
            }
        }
    }
}